<template>
    <!-- 
        1.dialog的显示与否是通过 :visible.sync的值来控制的
        2.close-on-click-modal="false"控制点击遮罩是否隐藏弹出层；
    -->
    <el-dialog title="添加菜单" width="560px" :visible.sync="addUserDialogVisible" :close-on-click-modal="false" :before-close="closeAndReset">
        <!-- rules表示绑定我们自定义的规则 -->
        <el-form :model="addUser" :rules="rules" ref="addForm">
            <el-radio-group v-model="addUser.type" @change="menuagain($event)">
                <el-radio  :label=0 >目录</el-radio>
                <el-radio  :label=1 >菜单</el-radio>
                <el-radio  :label=2 >按钮</el-radio>
            </el-radio-group>

<p v-if="this.addUser.type===0">

            <el-form-item label="目录标题" label-width="80px" prop="title" >
                <el-input v-model="addUser.title" autocomplete="off" placeholder="请输入标题；例：系统管理"></el-input>
            </el-form-item>
            <el-form-item label="目录标识" label-width="80px" prop="name">
                <el-input v-model="addUser.name" autocomplete="off" placeholder="请输入标识；例：SystemManager"></el-input>
            </el-form-item>
            <el-form-item label="目录路径" label-width="80px" prop="path">
                <el-input v-model="addUser.path" autocomplete="off" placeholder="请输入路径；例：/sys"></el-input>
            </el-form-item>
            <el-form-item label="排序编号" label-width="80px" prop="orderNum">
                <el-input-number v-model="addUser.orderNum" controls-position="right" @change="handleChange" :min="0" :max="10"></el-input-number>
            </el-form-item>
            <el-form-item label="目录图标" label-width="80px" prop="icon">
                <el-input v-model="addUser.icon" autocomplete="off" placeholder="请输入图标；例：el-icon-circle-plus"></el-input>
            </el-form-item>
</p>

            <p v-else-if="this.addUser.type===1">
            <el-form-item label="菜单标题" label-width="80px" prop="title">
                <el-input v-model="addUser.title" autocomplete="off" placeholder="请输入标题；例：用户管理"></el-input>
            </el-form-item>
            <el-form-item label="菜单标识" label-width="80px" prop="name">
                <el-input v-model="addUser.name" autocomplete="off" placeholder="请输入标识；例：user"></el-input>
            </el-form-item>
            <el-form-item label="上级菜单" label-width="80px" prop="parentId">

                <template>
                <el-select  v-model="addUser.parentId" placeholder="请选择">
                    <el-option 
                    v-for="item in menus"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id">
                    </el-option>
                </el-select>
                </template>


                <!-- <el-cascader  style="width:420px" label-width="100px"  autocomplete="off" v-model="addUser.parentId" :options="menus" :props="{ value: 'id', label: 'title', expandTrigger: 'hover' ,checkStrictly: true}"></el-cascader> -->
            </el-form-item>
            <el-form-item label="菜单路由" label-width="80px" prop="path">
                <el-input v-model="addUser.path" autocomplete="off" placeholder="例：/sys/user">
                    <template slot="prepend">{{parentUrl}}</template>
                </el-input>
            </el-form-item>
            <el-form-item
                prop="hidden"
                label="是否隐藏"
                width="180">
                <el-switch
                    v-model="addUser.hidden"
                    active-color="#40de5a"
                    inactive-color="#808080"
                    :active-value=true
                    :inactive-value=false>
                </el-switch>
            
    </el-form-item>
            <el-form-item label="排序编号" label-width="80px" prop="orderNum">
                <el-input-number v-model="addUser.orderNum" controls-position="right" @change="handleChange" :min="0" :max="10"></el-input-number>
            </el-form-item>
            <el-form-item label="菜单图标" label-width="80px" prop="icon">
                <el-input v-model="addUser.icon" autocomplete="off" placeholder="请输入图标；例：el-icon-circle-plus"></el-input>
            </el-form-item>
            </p>
            
         <p v-else>
            <el-form-item label="目录标题" label-width="80px" prop="title">
                <el-input v-model="addUser.title" autocomplete="off" placeholder="请输入标题；例：系统管理"></el-input>
            </el-form-item>
            <el-form-item label="上级菜单" label-width="80px" prop="parentId">
                <el-cascader  style="width:420px" label-width="100px"  autocomplete="off" v-model="addUser.parentId" :options="menus" :props="{ value: 'id', label: 'title', expandTrigger: 'hover' ,checkStrictly: true}"></el-cascader>
            </el-form-item>
            <el-form-item label="授权标识" label-width="80px" prop="name">
                <el-input v-model="addUser.name" autocomplete="off" placeholder="请输入标识；例：SystemManager"></el-input>
            </el-form-item>
         </p>         
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="closeAndReset">取 消</el-button>
            <el-button type="primary" @click="sureAdd">确 定</el-button>
        </div>
    </el-dialog>
</template>

<script>
// import axios from 'axios'
import  VueCookies  from 'vue-cookies'
import http from '@/utils/request.js'

export default {
    data() {
        return {
          
            // addUserDialogVisible是用来控制弹出框是否显示, false表示不显示；true表示显示
            addUserDialogVisible: false,
            addADDIFS:0,
            addUser: {
                title: '',
                name:'',
                path: '',
                icon:'',
                parentId:0,
                type:0,
                hidden:false,
                orderNum:0,


            },
            menus:[],
            // 定义校验规则
            rules: {
                title: [
                    // required表示该值必须要填；message 表示违反规则的提示信息；trigger表示何时出发
                    { required: false, trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                ],
                name: [
                    // required表示该值必须要填；message 表示违反规则的提示信息；trigger表示何时出发
                    { required: false, trigger: 'blur' },
                ],
                path: [
                    { required:false, trigger: 'blur' },
                ],
                icon: [
                    { required: false,trigger: 'blur' },
                ]
            }
        }
    },
    mounted(){
        // this.goto(this.radio)
     },    
    methods: {
        goto(type=0){
            http.get(`menu/${type}`).then(res => {
            // console.log(res)
            console.log('-----');
          this.menus = res
        })
        },
        // 确认添加
        async sureAdd() {
            if(this.addUser.type===2){

                this.addUser.parentId = this.addUser.parentId.reverse()[0]
                console.log(this.addUser.parentId)
            }else{
                if(this.addUser.type===0){
                    this.addUser.parentId = 0
                }
            }
               
               
                   
            this.addUser.createBy = VueCookies.get('username')
            try {
                await this.$refs.addForm.validate()
                await http.post(`usermenu`, this.addUser).then(res => {
            // console.log(res)
                console.log('-----');
                this.addADDIFS = res
            })

            if(this.addADDIFS===1){

                this.$message({ message: '添加成功', type: 'success', duration: 1000})

            }else{
                this.$message({ message: '添加失败，请检查是否有重名', type: 'info', duration: 1000})
            }
        
                this.addUserDialogVisible = false
             
                this.$refs.addForm.resetFields()
                
                this.$emit('refresh')
            }catch(err) {
                console.log(err);
            }
        },
        // 关闭并重置
        closeAndReset() {
            this.addUser.type = 0
            // 隐藏模态框
            this.addUserDialogVisible = false
            // 重置表单
            this.$refs.addForm.resetFields()
        },
        handleChange(value) {
            this.addUser.orderNum = value
        // console.log(value);
      },
      menuagain(radio){
        this.$refs.addForm.resetFields()
        // console.log(radio);
        this.goto(radio)
      }

    },
    computed:{
        parentUrl:function(){
            let path 
            this.menus.forEach(item =>{
                if (item.id == this.addUser.parentId) {
                    path = item.path
                }
            })
            return path
        }
    }
   
}
</script>

<style>

</style>